package com.sky.task;

import com.sky.entity.Orders;
import com.sky.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Component
@Slf4j
public class ScheduleTask {
    @Resource
    private OrderService orderService;
    @Scheduled(cron = "0 0/10 * * * ?")
    public void processTimeOutOrder(){
        //十分钟检查超时未支付的订单 将状态改为取消
        log.info("处理超时订单");
        //当前时间 - 15分钟 > 订单创建时间 且 订单状态为 待付款
        LocalDateTime time = LocalDateTime.now().minusMinutes(15);
        orderService.processExceptionOrder(time, Orders.PENDING_PAYMENT);

    }
    @Scheduled(cron = "0 0 2 * * ?")
    public void processDeliveryOrder(){
        //每晚两点检查前一天派送中的订单 将状态改为完成
        //当前时间-120分钟 > 订单创建时间 且 订单状态为 派送中
        log.info("处理派送中的订单");
        LocalDateTime time = LocalDateTime.now().minusMinutes(120);
        orderService.processExceptionOrder(time, Orders.DELIVERY_IN_PROGRESS);
    }
}
